using System;
using System.Collections.Generic;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;

namespace ClownFish
{
	public static class StringExtensions
	{
		public static bool IgnoreCaseStartsWith(this string string_0, string test)
		{
			return string_0.StartsWith(test, StringComparison.OrdinalIgnoreCase);
		}

		public static bool IgnoreCaseEndsWith(this string string_0, string test)
		{
			return string_0.EndsWith(test, StringComparison.OrdinalIgnoreCase);
		}

		public static int IgnoreCaseCompare(this string string_0, string test)
		{
			return string.Compare(string_0, test, StringComparison.OrdinalIgnoreCase);
		}

		public static int IgnoreCaseIndexOf(this string string_0, string test)
		{
			return string_0.IndexOf(test, StringComparison.OrdinalIgnoreCase);
		}

		public static int TryToInt(this string string_0, int defaultVal)
		{
			if (string.IsNullOrEmpty(string_0))
			{
				return defaultVal;
			}
			int result = defaultVal;
			int.TryParse(string_0, out result);
			return result;
		}

		public static int TryToInt(this string string_0)
		{
			return string_0.TryToInt(0);
		}

		public static decimal TryToDecimal(this string string_0, decimal defaultVal)
		{
			if (string.IsNullOrEmpty(string_0))
			{
				return defaultVal;
			}
			decimal result = defaultVal;
			decimal.TryParse(string_0, NumberStyles.Currency, null, out result);
			return result;
		}

		public static decimal TryToDecimal(this string string_0)
		{
			return string_0.TryToDecimal(0m);
		}

		public static DateTime TryToDateTime(this string string_0, DateTime defaultVal)
		{
			if (string.IsNullOrEmpty(string_0))
			{
				return defaultVal;
			}
			DateTime result = defaultVal;
			DateTime.TryParse(string_0, out result);
			return result;
		}

		public static DateTime TryToDateTime(this string string_0)
		{
			return string_0.TryToDateTime(DateTime.MinValue);
		}

		public static string GetMd5String(this string input, Encoding encoding)
		{
			if (input == null)
			{
				input = string.Empty;
			}
			byte[] value = new MD5CryptoServiceProvider().ComputeHash(encoding.GetBytes(input));
			return BitConverter.ToString(value).Replace("-", "");
		}

		public static string GetSha1String(this string input, Encoding encoding)
		{
			if (input == null)
			{
				input = string.Empty;
			}
			byte[] value = new SHA1CryptoServiceProvider().ComputeHash(encoding.GetBytes(input));
			return BitConverter.ToString(value).Replace("-", "");
		}

		public static bool IsGuid(this string string_0)
		{
			if (string_0 != null)
			{
				if (string_0.Length == 36)
				{
					bool result;
					try
					{
						new Guid(string_0);
						result = true;
					}
					catch
					{
						result = false;
					}
					return result;
				}
			}
			return false;
		}

		public static T TryChangeType<T>(this string string_0)
		{
			if (string_0 == null)
			{
				return default(T);
			}
			T result;
			try
			{
				result = (T)((object)Convert.ChangeType(string_0, typeof(T)));
			}
			catch
			{
				result = default(T);
			}
			return result;
		}

		public static object ConvertString(this string string_0, Type conversionType)
		{
			if (conversionType == null)
			{
				throw new ArgumentNullException("conversionType");
			}
			if (conversionType == TypeDefine.TypeString)
			{
				return string_0;
			}
			if (!string.IsNullOrEmpty(string_0))
			{
				if (conversionType.IsEnum)
				{
					return int.Parse(string_0);
				}
				if (conversionType.IsGenericType)
				{
					if (conversionType.GetGenericTypeDefinition() != TypeDefine.TypeNullable)
					{
						throw new InvalidCastException();
					}
					conversionType = conversionType.GetGenericArguments()[0];
				}
				if (conversionType == TypeDefine.TypeInt)
				{
					return int.Parse(string_0);
				}
				if (conversionType == TypeDefine.TypeLong)
				{
					return long.Parse(string_0);
				}
				if (conversionType == TypeDefine.TypeShort)
				{
					return short.Parse(string_0);
				}
				if (conversionType == TypeDefine.TypeDateTime)
				{
					return DateTime.Parse(string_0);
				}
				if (conversionType == TypeDefine.TypeBool)
				{
					return string.Compare(string_0, "false", StringComparison.OrdinalIgnoreCase) != 0 && !(string_0 == "0") && string_0.Length > 0;
				}
				if (conversionType == TypeDefine.TypeDouble)
				{
					return double.Parse(string_0);
				}
				if (conversionType == TypeDefine.TypeDecimal)
				{
					return decimal.Parse(string_0);
				}
				if (conversionType == TypeDefine.TypeFloat)
				{
					return float.Parse(string_0);
				}
				if (conversionType == TypeDefine.TypeGuid)
				{
					return new Guid(string_0);
				}
				if (conversionType == TypeDefine.TypeUlong)
				{
					return ulong.Parse(string_0);
				}
				if (conversionType == TypeDefine.TypeUint)
				{
					return uint.Parse(string_0);
				}
				if (conversionType == TypeDefine.TypeUshort)
				{
					return ushort.Parse(string_0);
				}
				if (conversionType == TypeDefine.TypeChar)
				{
					return string_0[0];
				}
				if (conversionType == TypeDefine.TypeByte)
				{
					return byte.Parse(string_0);
				}
				if (conversionType == TypeDefine.TypeSbyte)
				{
					return sbyte.Parse(string_0);
				}
				throw new InvalidCastException();
			}
			else
			{
				if (conversionType == TypeDefine.TypeDateTime)
				{
					return DateTime.MinValue;
				}
				if (conversionType == TypeDefine.TypeGuid)
				{
					return Guid.Empty;
				}
				if (conversionType.IsValueType && !conversionType.IsGenericType)
				{
					object result;
					try
					{
						result = Convert.ChangeType(0, conversionType);
					}
					catch
					{
						throw new InvalidCastException();
					}
					return result;
				}
				return null;
			}
		}

		public static byte[] HexToBin(string string_0)
		{
			if (string_0 == null)
			{
				throw new ArgumentNullException("hex");
			}
			if (string_0.Length % 2 != 0)
			{
				throw new InvalidOperationException("十六进制的字节数组的长度不正确。");
			}
			byte[] array = new byte[string_0.Length / 2];
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = byte.Parse(string_0.Substring(i * 2, 2), NumberStyles.HexNumber);
			}
			return array;
		}

		public static List<KeyValuePair<string, string>> SplitString(string line, char separator1, char separator2)
		{
			if (string.IsNullOrEmpty(line))
			{
				return new List<KeyValuePair<string, string>>();
			}
			string[] array = line.Split(new char[]
			{
				separator1
			}, StringSplitOptions.RemoveEmptyEntries);
			List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>(array.Length);
			char[] separator3 = new char[]
			{
				separator2
			};
			string[] array2 = array;
			for (int i = 0; i < array2.Length; i++)
			{
				string text = array2[i];
				string[] array3 = text.Split(separator3, StringSplitOptions.RemoveEmptyEntries);
				if (array3.Length != 2)
				{
					throw new ArgumentException("要拆分的字符串的格式无效。");
				}
				list.Add(new KeyValuePair<string, string>(array3[0], array3[1]));
			}
			return list;
		}

		public static List<int> StringToIntList(string string_0, char flag)
		{
			if (string.IsNullOrEmpty(string_0))
			{
				return new List<int>();
			}
			string[] array = string_0.Split(new char[]
			{
				flag
			}, StringSplitOptions.RemoveEmptyEntries);
			List<int> list = new List<int>(array.Length);
			string[] array2 = array;
			for (int i = 0; i < array2.Length; i++)
			{
				string s = array2[i];
				int item;
				if (int.TryParse(s, out item))
				{
					list.Add(item);
				}
			}
			return list;
		}
	}
}
